package com.example.springbootyczy.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootyczy.entity.ResultResponse;
import com.example.springbootyczy.entity.Student;
import com.example.springbootyczy.entity.UserLoginDTO;
import com.example.springbootyczy.service.IStudentService;
import com.example.springbootyczy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    IStudentService studentService;

    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/login")
    public ResultResponse<Student> login(@RequestBody UserLoginDTO userLoginDTO){
        redisUtil.set("demo", "123");
        redisUtil.expire("demo", 10);
        redisUtil.expire("name", 14);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Student::getName, userLoginDTO.getName())
                .and(wrapper -> {
                    wrapper.eq(Student::getPwd, userLoginDTO.getPwd());
                });
        Student student = studentService.getOne(queryWrapper);
        boolean flag = false;
        String code = "0";
        if(ObjectUtil.isNotEmpty(student)){
            flag = true;
            code = "1000";
        }else {
            code = "1001";
        }
        return ResultResponse.success(code, flag+"", student);
    }
}
